package hu.rsc.forbiddenwords.client.gui.entry;

import hu.rsc.forbiddenwords.client.Forbiddenwords;
import hu.rsc.forbiddenwords.client.util.Logger;
import hu.rsc.forbiddenwords.shared.EntryDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class EntryForm extends Composite {
    private EntryPanel parent;

    private FlexTable form = new FlexTable();

    private TextBox titleTb;
    Button save = new Button();
    Button newBt = new Button();

    private EntryDTO entry = new EntryDTO(0l);

    public EntryForm(EntryPanel parent) {
	this.parent = parent;
	buildContent();
	initWidget(this.form);
    }

    public EntryDTO getEntry() {
	return this.entry;
    }

    public void refresh(EntryDTO entry) {
	this.entry = entry;
	buildContent();
    }

    private void buildContent() {
	this.form.clear();
	this.form.addStyleName("form");
	this.form.addStyleName("border");
	int row = 0;

	Label lb = new Label("Forbidden words");
	lb.addStyleName("BoldLabel");
	this.form.setWidget(row, 0, lb);
	this.form.getFlexCellFormatter().setColSpan(row, 0, 2);
	this.form.getFlexCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);
	row++;

	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText("word");
	this.form.setWidget(row, 0, lb);
	this.titleTb = new TextBox();
	this.titleTb.addStyleName("forminput");
	this.form.setWidget(row, 1, this.titleTb);
	this.titleTb.setValue(this.entry.getWord());
	row++;

	HorizontalPanel buttonPanel = new HorizontalPanel();
	buttonPanel.addStyleName("buttonpanel");
	this.form.setWidget(row, 0, buttonPanel);
	this.form.getFlexCellFormatter().setColSpan(row, 0, 2);

	this.save.setText("Save");
	buttonPanel.add(this.save);

	this.save.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		if (validate()) {
		    EntryForm.this.parent.save(EntryForm.this.entry);
		}
	    }
	});

	this.newBt.setText("New");
	buttonPanel.add(this.newBt);
	this.newBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		refresh(new EntryDTO(0l));
	    }
	});
	setEditable();
    }

    private void setEditable() {
	this.titleTb.setEnabled(Forbiddenwords.getSelectedApplication().isEditable());
	this.save.setEnabled(Forbiddenwords.getSelectedApplication().isEditable());
	this.newBt.setEnabled(Forbiddenwords.getSelectedApplication().isEditable());
    }

    private boolean validate() {
	this.entry.setWord(this.titleTb.getValue());
	this.entry.setApplication(Forbiddenwords.getSelectedApplication().getId());
	StringBuilder error = new StringBuilder();
	if (this.entry.getWord() == null || this.entry.getWord().length() == 0) {
	    error.append("The word is empty.");
	}
	if (this.entry.getApplication() == null || this.entry.getApplication().equals(new Long(0l))) {
	    error.append("There is no selected application.");
	}

	if (error.length() > 0) {
	    Logger.logError(error.toString());
	    return false;
	}
	return true;
    }

}
